@extends('layout.page')
@section('title','渠道 - 列表')

@section('static-page')
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>渠道 <small>列表</small></h5>
                </div>
                <div class="ibox-content">
                    <table class="table table-bordered text-center">
                        <thead class="text-center">
                        <tr>
                            <th>渠道代码</th>
                            <th>合作方式</th>
                            <th>合作目标</th>
                            <th>预期效果</th>
                            <th>审核状态</th>
                            <th>渠道状态</th>
                            <th>总成交金额</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($channel_list as $channel)
                            <tr>
                                <td>{{$channel['channel_code']}}</td>
                                <td>{{$channel['cooperation']['name']}}</td>
                                <td>{{$channel['target']['name']}}</td>
                                <td>{{$channel['expections']}}</td>
                                <td>{{$channel['check_status']['name']}}</td>
                                <td>
                                    <select class="_open_status" data-id="{{ $channel['id'] }}">
                                        @foreach($open_status as $v)
                                            <option value="{{ $v['id'] }}"
                                                @if($v['id'] == $channel['open_status']['id']) selected @endif>
                                                {{ $v['name'] }}</option>
                                        @endforeach
                                    </select>
                                </td>
                                <td>{{$channel['invest_num']}}</td>
                                <td>
                                    <a href="/ceb/channels/check?channel_id={{ $channel['id'] }}">审核</a>
                                    @if($channel['check_status']['id'] == \App\Constants\ChannelCheckStatus::PASS)
                                        @if($channel['settlement_status'] == \App\Constants\ChannelSettlementStatus::UNSETTLED)
                                            | <a href="javascript:;" class="_fund" data-id="{{ $channel['id'] }}">结算</a>
                                        @endif
                                        | <a href="javascript:;" class="btn_mark" data-id="{{ $channel['id'] }}">备注</a>
                                        | <a href="/ceb/channels/daily?channel_id={{$channel['id']}}">数据跟踪</a>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    @include('wiget.pager',[
                        'page' => $page,
                        'page_size' => $page_size,
                        'count' => $count,
                        'show_count' => 10,
                        'path' => url('/ceb/channels/index?channel_provider_id=' . $channel_provider_id)
                    ])
                    <a class="btn btn-sm btn-primary" href="/ceb/channels/create?channel_provider_id={{ $channel_provider_id }}">添加渠道代码</a>
                </div>
            </div>
        </div>
        @endsection
@section('modals')
<div class="modal fade" id="settle_dialog" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">费用结算</h4>
            </div>
            <div class="modal-body">
                <table class="table table-bordered text-center">
                    <thead class="text-center">
                    <tr>
                        <th>合同类型</th>
                        <th>合同金额</th>
                        <th>实际结算金额</th>
                        <th>备注</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td class="contract_type"></td>
                            <td class="contract_amount"></td>
                            <td><input type="text" class="pay_amount form-control"></td>
                            <td><input type="text" class="_mark form-control"></td>
                            <td><button class="btn btn-primary btn_save">保存</button></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="mark_dialog" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">备注</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="mark-list"></div>
                    <div class="mark-new">
                        <div class="col-sm-2">添加备注</div>
                        <div class="col-sm-10">
                            <textarea class="form-control" rows="5" id="mark-content"></textarea>
                        </div>
                        <button class="btn btn-primary btn_save">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
@section('javascript')
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
        $('._open_status').on('change',function(){
            var id = $(this).data('id');
            var status = $(this).val();
            toAjax('/ceb/channels/openstatus',{channel_id:id,status:status},'POST')
                    .on('hxop.ajax-success',function(evt,data){
                        parent.layer.msg('成功!');
                    });
        });
        $('._fund').on('click',function(){
            var id = $(this).data('id');
            toAjax('/ceb/channels/show',{id:id},'GET')
                    .on('hxop.ajax-success',function(evt,data){
                        showSettleDialog(id,data.contract_type,data.contract_amount);
                    });
        });

        $('.btn_mark').on('click',function(){
            var id = $(this).data('id');
            toAjax('/ceb/channels/marks',{channel_id:id},'GET')
                    .on('hxop.ajax-success',function(evt,data){
                        showMarkDialog(data.list,data.channel);
                    });
        });

        function showSettleDialog(id,contract_type,contract_amount){
            var dom = $('#settle_dialog');
            $('.contract_type',dom).html(contract_type);
            $('.contract_amount',dom).html(contract_amount);
            $('.btn_save',dom).off('click').on('click',function(){
                var pay_amount = $('.pay_amount',dom).val();
                var mark = $('._mark',dom).val();
                toAjax('/ceb/channels/settle',{id:id,pay_amount:pay_amount,settlement_mark:mark},'POST')
                        .on('hxop.ajax-success',function(){
                            layer.msg('成功!');
                            location.reload();
                        });
            });
            dom.modal();
        }

        function showMarkDialog(list,channel){
            var dom = $('#mark_dialog');
            if(Validator.arr_not_empty(list)){
                var s = '<div class="col-md-2">备注</div>';
                s += '<div class="col-md-10 h200 overflow-scroll">'
                $.each(list,function(k,v){
                    s += '<div>' + v.content + '</div>';
                    s += '<div class="text-right grey-0">' + v.created_at + '</div>';
                    s += '<hr>';
                });
                s += '</div>';
                $('.mark-list').html(s);
            }
            $('.btn_save',dom).off('click').on('click',function(){

                var content = $('#mark-content').val();
                if(Validator.is_empty(content)){
                    alert('备注不能为空');
                    return;
                }

                toAjax('/ceb/channels/marks',{channel_id:channel.id,content:content},'POST')
                        .on('hxop.ajax-success',function(){
                            layer.msg('成功!');
                            location.reload();
                        });
            });
            dom.modal();
        }
    });
</script>
@endsection
